<%@ page import="java.net.URLDecoder" %>
<%@ page import="com.google.gson.Gson" %>
<%@ page import="com.zjitc.model.User" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/3/31 0031
  Time: 10:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"
          type="text/css"/>
    <script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"
            type="text/javascript"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap.min.js"
            type="text/javascript"></script>
    <!-- 引入自定义css文件 style.css -->
    <style>
        nav {
            background-color: antiquewhite;
            color: black;
        }
    </style>
</head>
<body>
<!--
            时间：2015-12-30
            描述：菜单栏
        -->
<div class="container-fluid">
    <div class="col-md-4">
        <img src="${pageContext.request.contextPath}/img/logogo.png"/>
    </div>
    <div class="col-md-5">
        <img src="${pageContext.request.contextPath}/img/headps.jpg"/>
    </div>
    <div class="col-md-3" style="padding-top:20px">
        <ol class="list-inline">
            <c:if test="${empty sessionScope.user}">
                <li><a href="${pageContext.request.contextPath}/jsp/login.jsp">登录</a></li>
                <li><a href="${pageContext.request.contextPath}/jsp/register.jsp">注册</a></li>
            </c:if>

            <c:if test="${not empty sessionScope.user}">
                <li>欢迎回来: ${sessionScope.user.username}</li>
                <li><a href="${pageContext.request.contextPath}/user/loginOut.do">注销</a></li>
                <li><a href="${pageContext.request.contextPath}/car/findAll.do">购物车</a></li>
                <li><a href="${pageContext.request.contextPath}/order/orderList.do">我的订单</a></li>
                <li><!-- 按钮触发模态框 -->
                    <button id="addressButton" class="btn btn-primary btn-lg" data-toggle="modal"
                            data-target="#myModal">
                        收货地址
                    </button>
                </li>
                <li><!-- 按钮触发模态框 -->
                    <button id="chat" class="btn btn-primary btn-lg" data-toggle="modal">
                        用户交流中心
                    </button>
                </li>

            </c:if>
        </ol>
    </div>
</div>
<!--

<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel" align="center">
                    收货地址
                </h4>
            </div>
            <form class="form-horizontal">
                <div class="form-group">
                    <label for="address" class="col-sm-4 control-label">地址</label>
                    <div class="col-sm-5">
                        <input type="text" class="form-control" id="address" placeholder="请输入收货地址">
                    </div>
                </div>
                <div class="form-group">
                    <label for="consignee" class="col-sm-4 control-label">收货人</label>
                    <div class="col-sm-5">
                        <input type="text" class="form-control" id="consignee" placeholder="请输收货人">
                    </div>
                </div>
                <div class="form-group">
                    <label for="telephone" class="col-sm-4 control-label">电话</label>
                    <div class="col-sm-5">
                        <input type="text" class="form-control" id="telephone" placeholder="请输入联系方式">
                    </div>
                    <a href="javascript:void(0)" id="sendMessage" onclick="sendMessage()">绑定手机</a>
                </div>
                <div class="form-group" id="vaCode">
                    <label for="telephone" class="col-sm-4 control-label">验证码</label>
                    <div class="col-sm-5">
                        <input type="text" class="form-control" id="code" placeholder="请输入验证码">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <input type="button" value="修改收货地址" id="addressUpdate" class="btn btn-primary"
                           data-dismiss="modal">

                    </input>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<%--时间：2015-12-30--%>
<%--描述：导航条 navbar-inverse--%>
<%---->--%>
<div class="container-fluid">
    <nav class="navbar">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="${pageContext.request.contextPath}/index.do">首页</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav" id="listOfNav">

                </ul>
                <form class="navbar-form navbar-right" role="search" action="/product/findLike.do">
                    <div class="form-group">
                        <input type="text" class="form-control" name="name" placeholder="Search">
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>

            </div>
        </div>

    </nav>
</div>
</body>

<%--<%--%>
<%--Cookie[] cookies = request.getCookies();--%>
<%--if (cookies != null && cookies.length > 0) {--%>
<%--for (Cookie cookie : cookies) {--%>
<%--String cookieName = cookie.getName();--%>
<%--if ("user".equals(cookieName)) {--%>
<%--String s = cookie.getValue();--%>
<%--String ss = URLDecoder.decode(s, "utf-8");--%>
<%--Gson gson = new Gson();--%>
<%--User user = gson.fromJson(ss, User.class);--%>
<%--session.setAttribute("user", user);--%>
<%--out.println(user);--%>
<%--System.out.println("哇啊阿发啊cookie" + user);--%>
<%--}--%>
<%--}--%>
<%--}--%>
<%--%>--%>
<script>
    $.get(
        "${pageContext.request.contextPath}/category/findAllCategory.do",
        "",
        function (dt, status, xhr) {
            for (var i = 0; i < dt.length; i++) {
                var o = dt[i];
                var cid = dt[i].cid;
                var name = dt[i].name;
                $("#listOfNav").append("<li><a href=\"${pageContext.request.contextPath}/product/findCatchByCidAndPid.do?cid="
                    + cid + "\">" + name + "</a></li>")
            }
            $("#listOfNav").append("<li id='productFlag'><a href=\"${pageContext.request.contextPath}/product/findProductByFlag.do?flag="
                + 1 + "\">秒杀活动 </a></li>")
        },
        "json"
    );

    $.get(
        "${pageContext.request.contextPath}/product/verifyFlag.do",
        "",
        function (ms, status, xhr) {
            if (ms == -1) {
                $("#productFlag").remove();
            }
        },
        "json"
    );

    function sendMessage() {
        $.ajax(
            {
                url: "/sendMessage.do",
                data: {
                    telephone: $("#telephone").val()
                },
                success: function () {
                }
            }
        )
    }

    $("#addressUpdate").click(function () {
        var s = "增加收货地址";
        if ($("#addressUpdate").val() == s) {
            addAddress();
        } else {
            updateAddress();
        }
    })

    function updateAddress() {
        $.ajax(
            {
                url: "/updateAddress.do",
                data: {
                    address: $("#address").val(),
                    consignee: $("#consignee").val(),
                    telephone: $("#telephone").val()
                },
                success: function () {

                }
            }
        )
    }

    function addAddress() {
        $.ajax(
            {
                url: "/addAddress.do",
                data: {
                    address: $("#address").val(),
                    consignee: $("#consignee").val(),
                    telephone: $("#telephone").val(),
                    code: $("#code").val()
                },
                success: function (msg) {
                }
            }
        )
    }

    $("#addressButton").click(function () {
        $.ajax(
            {
                url: "/findOne.do",
                success: function (msg) {
                    if (msg != "") {
                        $("#address").val(msg.shippingAddress);
                        $("#consignee").val(msg.shippingName)
                        $("#telephone").val(msg.shippingTelephone)
                        $("#vaCode").css("display", "none")
                        $("#sendMessage").css("display", "none")
                        $("#addressUpdate").val("修改收货地址")
                    }
                    else {
                        $("#vaCode").css("display", "block")
                        $("#sendMessage").css("display", "block")
                        $("#addressUpdate").val("增加收货地址")//input里面的value数值
                    }
                }
            }
        )
    })

    $("#chat").click(function () {
        $(window).attr("location", "${pageContext.request.contextPath }/jsp/chat.jsp");
    })

</script>
</html>
